<!--
  Copyright 2018 The Outline Authors

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<link rel='import' href='../bower_components/polymer/polymer.html'>
<link rel='import' href='../bower_components/paper-button/paper-button.html'>
<link rel='import' href='../bower_components/iron-icon/iron-icon.html'>
<link rel='import' href='../bower_components/iron-icons/iron-icons.html'>

<link rel="import" href="./cloud-install-styles.html">
<link rel="import" href="./cities.html">
<link rel="import" href="./outline-step-view.html">

<dom-module id="outline-region-picker-step">
  <template>
    <style include="cloud-install-styles"></style>

    <style>
      :host {
        text-align: center;
        --primary-green: #1DE9B6;
      }
      input[type="radio"] {
        display: none;
      }
      input[type="radio"]:checked+label.city-button {
        background: #FFFFFF;
        box-shadow: 0 0 2px 0 rgba(0,0,0,0.14), 0 2px 2px 0 rgba(0,0,0,0.12), 0 1px 3px 0 rgba(0,0,0,0.20);
        border-radius: 4px;
        border: 1px solid var(--primary-green);
      }
      input[type="radio"]+label.city-button:hover {
        border: 1px solid var(--primary-green);
      }
      input[type="radio"]+label.city-button {
        display: inline-block;
        width: 22%;
        min-width: 100px;
        position: relative;
        margin: 3px;
        padding-top: 24px;
        text-align: center;
        border: 1px solid;
        border-color: rgba(0, 0, 0, .00);
        cursor: pointer;
        transition: .5s;
        background: #FFFFFF;
        box-shadow: 0 0 2px 0 rgba(0,0,0,0.14), 0 2px 2px 0 rgba(0,0,0,0.12), 0 1px 3px 0 rgba(0,0,0,0.20);
        border-radius: 4px;
      }
      input[type="radio"]:disabled+label.city-button {
        filter: grayscale(100%);
      }
      .city-name {
        color: rgba(0, 0, 0, 0.87);
        font-size: 16px;
        line-height: 19px;
        padding: 36px 0 24px 0;
      }
      paper-button {
        margin-top: 24px;
        width: 95%;
        background: var(--primary-green);
        color: rgba(0, 0, 0, 0.87);
      }
      .flag {
        width: 86px;
        height: 86px;
      }
      .card-content {
        padding: 24px 12px 0 12px;
      }
      iron-icon {
        color: var(--primary-green);
        position: absolute;
        top: 0;
        right: 0;
        margin: 6px;
      }
    </style>
      <outline-step-view>
        <span slot="step-title">Select the location of your server.</span>
        <span slot="step-description">This is where your internet experience<br/> will come from.</span>
        <div class='card-content' id="cityContainer">
          <template is="dom-repeat" items="{{cities}}">
            <input type='radio' city$='{{item}}' name='city' id$='{{item}}' disabled$='{{!isAvailable(availableRegionIds, item)}}' on-change='citySelected'>
              <label for$='{{item}}' class='city-button'>
                <iron-icon icon="check-circle" hidden$="{{!_isSelectedCity(selectedCityId, item)}}"></iron-icon>
                <img class='flag' src$='{{getFlag(item)}}'>
                <div class="city-name">{{getCityName(item)}}</div>
              </label>
            </input>
          </template>
          <paper-button id="createServerButton" on-tap='handleCreateServerTap' disabled$='[[!isCreateButtonEnabled(creatingServer, selectedCityId)]]'>Set up server</paper-button>
        </div>
      </outline-step-view>
  </template>
  <script>
    const FLAG_IMAGE_DIR = 'images/flags';
    const flagById = {
      ams: `${FLAG_IMAGE_DIR}/netherlands.png`,
      sgp: `${FLAG_IMAGE_DIR}/singapore.png`,
      blr: `${FLAG_IMAGE_DIR}/india.png`,
      fra: `${FLAG_IMAGE_DIR}/germany.png`,
      lon: `${FLAG_IMAGE_DIR}/uk.png`,
      sfo: `${FLAG_IMAGE_DIR}/us.png`,
      tor: `${FLAG_IMAGE_DIR}/canada.png`,
      nyc: `${FLAG_IMAGE_DIR}/us.png`
    };

    Polymer({
      is: 'outline-region-picker-step',
      properties: {
        cities: {
          type: Array,
          value: Object.keys(flagById)
        },
        availableRegionIds: {  // One-to-one map from cityIds to regionIds.
          type: Object,
          value: {}
        },
        selectedCityId: String,
        creatingServer: Boolean
      },
      handleCreateServerTap: function () {
        this.creatingServer = true;
        this.fire('RegionSelected');
      },
      getCityName: function (cityId) {
        return cityEnglishNameById[cityId] || '';
      },
      getFlag: function (cityId) {
        return flagById[cityId] || '';
      },
      isAvailable: function (map, cityId) {
        if (!map) {
          return false;
        }
        return cityId in map;
      },
      citySelected: function (event) {
        this.selectedCityId = event.model.get('item');
      },
      _isSelectedCity: function(selectedCityId, cityId) {
        if (!selectedCityId) {
          return false;
        }
        return selectedCityId === cityId;
      },
      getSelectedRegionId: function() {
        return this.availableRegionIds[this.selectedCityId];
      },
      init: function() {
        this.creatingServer = false;
        this.selectedCityId = null;
        // Ensure that no radio button is checked.
        const checkedCityElement = this.$.cityContainer.querySelector('input[name="city"]:checked');
        if (checkedCityElement) {
          checkedCityElement.checked = false;
        }
      },
      isCreateButtonEnabled: function(creatingServer, selectedCityId) {
        return !creatingServer && selectedCityId;
      }
    });
  </script>
</dom-module>
